$(function() {
	(function($) {
		try {
			var myDiagram =
	        $(go.Diagram, "myDiagramDiv",
	          {
	            initialContentAlignment: go.Spot.Center,
	            validCycle: go.Diagram.CycleNotDirected,  // don't allow loops
	            // For this sample, automatically show the state of the diagram's model on the page
	            "ModelChanged": function(e) {
	                if (e.isTransactionFinished) showModel();
	              },
	            "undoManager.isEnabled": true
	          });

	      // This template is a Panel that is used to represent each item in a Panel.itemArray.
	      // The Panel is data bound to the item object.
	      var fieldTemplate =
	        $(go.Panel, "TableRow",  // this Panel is a row in the containing Table
	          new go.Binding("portId", "name"),  // this Panel is a "port"
	          {
	            background: "transparent",  // so this port's background can be picked by the mouse
	            fromSpot: go.Spot.Right,  // links only go from the right side to the left side
	            toSpot: go.Spot.Left,
	            // allow drawing links from or to this port:
	            fromLinkable: true, toLinkable: true
	          },
	          $(go.Shape,
	            { width: 24, height: 24, column: 0, strokeWidth: 2, margin: 4,
	              // but disallow drawing links from or to this shape:
	              fromLinkable: false, toLinkable: false },
	            new go.Binding("figure", "figure"),
	            new go.Binding("fill", "color")),
	          $(go.TextBlock,
	            { margin: new go.Margin(0, 5), column: 1, font: "bold 13px sans-serif",
	              alignment: go.Spot.Left,
	              // and disallow drawing links from or to this text:
	              fromLinkable: false, toLinkable: false },
	            new go.Binding("text", "name")),
	          $(go.TextBlock,
	            { margin: new go.Margin(0, 5), column: 2, font: "13px sans-serif", alignment: go.Spot.Left },
	            new go.Binding("text", "info"))
	        );

	      // This template represents a whole "record".
	      myDiagram.nodeTemplate =
	        $(go.Node, "Auto",
	          { movable: false,
	            copyable: false,
	            deletable: false },
	          new go.Binding("location", "loc", go.Point.parse).makeTwoWay(go.Point.stringify),
	          // this rectangular shape surrounds the content of the node
	          $(go.Shape,
	            { fill: "#EEEEEE" }),
	          // the content consists of a header and a list of items
	          $(go.Panel, "Vertical",
	            // this is the header for the whole node
	            $(go.Panel, "Auto",
	              { stretch: go.GraphObject.Horizontal },  // as wide as the whole node
	              $(go.Shape,
	                { fill: "#1570A6", stroke: null }),
	              $(go.TextBlock,
	                {
	                  alignment: go.Spot.Center,
	                  margin: 3,
	                  stroke: "white",
	                  textAlign: "center",
	                  font: "bold 12pt sans-serif"
	                },
	                new go.Binding("text", "key"))),
	            // this Panel holds a Panel for each item object in the itemArray;
	            // each item Panel is defined by the itemTemplate to be a TableRow in this Table
	            $(go.Panel, "Table",
	              {
	                padding: 2,
	                minSize: new go.Size(100, 10),
	                defaultStretch: go.GraphObject.Horizontal,
	                itemTemplate: fieldTemplate
	              },
	              new go.Binding("itemArray", "fields")
	            )  // end Table Panel of items
	          )  // end Vertical Panel
	        );  // end Node

	      myDiagram.linkTemplate =
	        $(go.Link,
	          {
	            relinkableFrom: true, relinkableTo: true, // let user reconnect links
	            toShortLength: 4,  fromShortLength: 2
	          },
	          $(go.Shape, { strokeWidth: 1.5 }),
	          $(go.Shape, { toArrow: "Standard", stroke: null })
	        );
	      var jqxhr = jQuery.get( "draw/mapperModel.do")
    	  .done(function(data) {
    		  myDiagram.model = go.Model.fromJson(data);
    		  loadDiagramProperties();
    	  })
    	  .fail(function() {
    	    alert( "init flow model error" );
    	  });
	      showModel();  // show the diagram's initial model

	      function showModel() {
	        var text = myDiagram.model.toJson();
	        var jqxhr = jQuery.post( "draw/linksModel.do",{"gojs":text})
	        	  .done(function(data) {
	        		  jQuery("#sourceRuleView").html(data);
	        	  })
	        	  .fail(function() {
	        	    alert( "error" );
	        	  });
	      }
	      window.freshMapperView = function (phaseId, mapperJson) {
	    	  if (phaseId == null || mapperJson == null || mapperJson == "") {
	    		  mapperJson = '{"linkFromPortIdProperty": "fromPort", "linkToPortIdProperty": "toPort", "nodeDataArray": [], "linkDataArray": [],"class": "go.GraphLinksModel"}';
	    	  }
	    	  myDiagram.model = go.Model.fromJson(mapperJson);
    		  loadDiagramProperties();
	      }
		} catch (e) {alert(e.message);}
		})(go.GraphObject.make);
});

